<?php



namespace app\modules\admin\models;

use Yii;
use yii\web\IdentityInterface;
use yii\helpers\Url;
use yii\base\Exception;
/**
 * ContactForm is the model behind the contact form.
 */

class PreAuthorization
{
    //确认卡信息
    public function carinforequre($carcode)
    {
        //交易类型编码
        $txnType = 'INP';
        //信用卡卡号
        $cardNo = $carcode;//'4392********4048';
        //接口版本号
        $version = '1.0';

        $reqXml = '<?xml version="1.0" encoding="UTF-8"?><MasMessage xmlns="http://www.99bill.com/mas_cnp_merchant_interface"><version>'.$version.'</version><QryCardContent><txnType>'.$txnType.'</txnType><cardNo>'.$cardNo.'</cardNo></QryCardContent></MasMessage>';
        $clientcert    ="/home/flightsharecar/user_upload/php_demo/cert.pem";
        $clientprikey="/home/flightsharecar/user_upload/php_demo/private.pem";
        $tuCurl = curl_init();
        $merchantId='812330945110001';//商户编号
        $certPassword='vpos123';//证书密码
        $loginKey='vpos123';//商户登录密码
        $loginInfo = array(
            "Authorization: Basic " . base64_encode($merchantId . ":" . $loginKey)
        );
        curl_setopt($tuCurl, CURLOPT_URL, "https://sandbox.99bill.com:9445/cnp/query_cardinfo");

        curl_setopt($tuCurl, CURLOPT_HTTPHEADER, $loginInfo);//请求头中包含merchantId和对应的密码的内容
        //curl_setopt($tuCurl, CURLOPT_USERPWD, $loginKey);//登录用户密码

        curl_setopt($tuCurl,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($tuCurl,CURLOPT_SSL_VERIFYHOST,false);


        curl_setopt($tuCurl,CURLOPT_SSLCERTTYPE,'PEM');//证书格式
        curl_setopt($tuCurl,CURLOPT_SSLCERT,$clientcert);//证书路径
        curl_setopt($tuCurl,CURLOPT_SSLCERTPASSWD,$certPassword);//证书密码

        curl_setopt($tuCurl,CURLOPT_SSLKEYTYPE,'PEM');//本地私钥格式
        curl_setopt($tuCurl,CURLOPT_SSLKEY,$clientprikey);//私钥文件路径

        curl_setopt($tuCurl, CURLOPT_POST, 1);//POST
        curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $reqXml);//POST内容中包含报文


        curl_setopt($tuCurl,CURLOPT_TIMEOUT,30);

        curl_setopt($tuCurl,CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($ch,CURLOPT_URL,$url);

        $tuData=curl_exec($tuCurl);
        $obj=simplexml_load_string($tuData,'SimpleXMLElement', LIBXML_NOCDATA);
        if(is_object($obj)){
            $obj=get_object_vars($obj);
        }
        $flag=2;
        if(array_key_exists("CardInfoContent",$obj))
        {
            $array2=$this->std_class_object_to_array($obj["CardInfoContent"]);
            if(is_array($array2) && array_key_exists("validFlag",$array2))
            {
                $flag=$array2["validFlag"];
            }
        }

        return $flag;
    }

    public static function std_class_object_to_array($stdclassobject)
    {
        $_array = is_object($stdclassobject) ? get_object_vars($stdclassobject) : $stdclassobject;
        foreach ($_array as $key => $value) {
            $value = (is_array($value) || is_object($value)) ? std_class_object_to_array($value) : $value;
            $array[$key] = $value;
        }
        return $array;
    }




}


